﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OData.Edm;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using server.status_db;
namespace server
{
    public class UserResolverService  
{
    private readonly IHttpContextAccessor _context;
    public UserResolverService(IHttpContextAccessor context)
    {
        _context = context;
    }

    public string GetServer()
    {
       var claim =  _context.HttpContext.User?.Claims?.FirstOrDefault(c => c.Type == "server");
       if(claim != null)
        return claim.Value;
       else
        return _context.HttpContext.Request.Query["server"];
    }
}
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            
        }
        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddDbContext<SampleContext>(options =>options.UseSqlServer(@"Data Source=10.30.252.76;Initial Catalog=test;User ID=sa;Password=hyg&197925;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"));
            services.AddScoped<UserResolverService>();
            services.AddDbContext<status_oneContext>();
            services.AddHttpContextAccessor();
             services.AddResponseCompression(options =>
     {          
         
         options.Providers.Add<BrotliCompressionProvider>();
         options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "image/svg+xml" });
     });
       services.Configure<BrotliCompressionProviderOptions>(options => 
    {
        options.Level = CompressionLevel.Fastest;
    });
            services.AddDistributedMemoryCache();
            services.AddSession();
            services.AddOData();

               var key = Encoding.ASCII.GetBytes("aaaaaaaaaabbbbbbbbb");
            services.AddAuthentication(x =>
            {
                 
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata = false;
                x.SaveToken = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });
           
               services.AddMvc(options =>
            {
               
                options.EnableEndpointRouting = false;
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options =>
              {  options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
             options.SerializerSettings.ContractResolver = new DefaultContractResolver();}); ;
      
            // configure DI for application services
            //services.AddScoped<IUserService, UserService>();   
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,  ILoggerFactory loggerFactory)
        {
        
            loggerFactory.AddFile("myapp-{Date}.log");
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
             app.UseResponseCompression();
            app.UseCors(x => x
                            .AllowAnyOrigin()
                            .AllowAnyMethod()
                            .AllowAnyHeader());
            app.UseAuthentication();

            app.UseMvc(b =>
               {
                    b.SetTimeZoneInfo(TimeZoneInfo.Utc);
                   b.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
                   b.MapODataServiceRoute("odata", "api", GetEdmModel());

               });
           

            app.UseStaticFiles(new StaticFileOptions()
            {  
            　　FileProvider = new PhysicalFileProvider(
            　　 Directory.GetCurrentDirectory()+ @"/template/"),
            　　RequestPath = new PathString("/template")
            });
            app.UseStaticFiles(new StaticFileOptions()
            {  
            　　FileProvider = new PhysicalFileProvider(
            　　 Directory.GetCurrentDirectory()+ @"/dist/"),
            　　RequestPath = new PathString("")
            });
             app.UseStaticFiles(new StaticFileOptions()
            {  
            　　FileProvider = new PhysicalFileProvider(
            　　 Directory.GetCurrentDirectory()+ @"/help/"),
            　　RequestPath = new PathString("/help")
            });
             app.UseDefaultFiles();
        }
        private static IEdmModel GetEdmModel()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet<Set_B_ALFX>("Alfxs");
            builder.EntitySet<PG_B_MC>("Mcs");
            builder.EntitySet<PG_B_SSMZJSQK>("Ssmzjsqks");
            builder.EntitySet<PG_B_LX>("Lxs");
            builder.EntitySet<PG_B_ZSJH>("Zsjhs");
            builder.EntitySet<PG_B_ZSFS>("Zsfss");
            builder.EntitySet<PG_B_ZXS>("Zxss");
     
            builder.EntitySet<PG_B_JBQK>("Jbqks");

            builder.EntityType<PG_B_JBQK>().Collection
        .Action("BulkAddJbqks")
        .CollectionParameter<PG_B_JBQK>("Jbqks");

            builder.EntitySet<PG_B_CYJX>("Cyjxs");

            builder.EntityType<PG_B_CYJX>().Collection
        .Action("BulkAddCYJX")
        .CollectionParameter<PG_B_CYJX>("CYJX");

            builder.EntitySet<PG_B_XNZRJBQK>("Xnqk");
            builder.EntitySet<PG_B_XNZRSKQK>("Xnsq");
            builder.EntitySet<PG_B_XNZRQTQK>("Xnqt");
            builder.EntitySet<PG_B_XNJKJBQK>("Jkqk");
            builder.EntitySet<PG_B_XNJKSKQK>("Jksq");
            builder.EntitySet<PG_B_XNJKQTQK>("Jkqt");
            builder.EntitySet<PG_B_XWJZJBQK>("Xwqk");
            builder.EntitySet<PG_B_XWJZSKQK>("Xwsq");
            builder.EntitySet<PG_B_XWJKJBQK>("Xwjkjb");
            builder.EntitySet<PG_B_XWJKSKQK>("Xwjksq");
            builder.EntitySet<PG_B_JFSRMX1>("Jfsrmx1s");


            builder.EntityType<PG_B_JFSRMX1>().Collection
          .Action("BulkAddJfsrmx1")
          .CollectionParameter<PG_B_JFSRMX1>("Jfsrmx1");
            builder.EntitySet<PG_B_JFSRMX2>("Jfsrmx2s");

            builder.EntityType<PG_B_JFSRMX2>().Collection
          .Action("BulkAddJfsrmx2")
          .CollectionParameter<PG_B_JFSRMX2>("Jfsrmx2");
            builder.EntitySet<PG_B_JFSRMX3>("Jfsrmx3s");

            builder.EntityType<PG_B_JFSRMX3>().Collection
          .Action("BulkAddJfsrmx3")
          .CollectionParameter<PG_B_JFSRMX3>("Jfsrmx3");
            builder.EntitySet<PG_B_JFSRMX4>("Jfsrmx4s");
            builder.EntitySet<PG_B_JFSRMX5>("Jfsrmx5s");
            builder.EntitySet<PG_B_JFSR>("Jfsrs");
            builder.EntitySet<PG_B_JFZCMX1>("Jfzcmx1s");

            builder.EntityType<PG_B_JFZCMX1>().Collection
          .Action("BulkAddJfzcmx1")
          .CollectionParameter<PG_B_JFZCMX1>("Jfzcmx1");
            builder.EntitySet<PG_B_JFZCMX2>("Jfzcmx2s");

            builder.EntityType<PG_B_JFZCMX2>().Collection
          .Action("BulkAddJfzcmx2")
          .CollectionParameter<PG_B_JFZCMX2>("Jfzcmx2");
            builder.EntitySet<PG_B_JFZCMX3>("Jfzcmx3s");
            builder.EntitySet<PG_B_JFZC>("Jfzcs");
            builder.EntityType<JS_B_教师授课情况>().Collection
        .Action("BulkAddJsskqk")
        .CollectionParameter<JS_B_教师授课情况>("Jsskqk");
            builder.EntityType<JS_B_教师其他情况A>().Collection
        .Action("BulkAddJsqtqkA")
        .CollectionParameter<JS_B_教师其他情况A>("JsqtqkA");
            builder.EntityType<JS_B_教师其他情况B>().Collection
        .Action("BulkAddJsqtqkB")
        .CollectionParameter<JS_B_教师其他情况B>("JsqtqkB");
            builder.EntityType<JS_B_教师其他情况C>().Collection
        .Action("BulkAddJsqtqkC")
        .CollectionParameter<JS_B_教师其他情况C>("JsqtqkC");
            builder.EntityType<JS_B_教师其他情况D>().Collection
        .Action("BulkAddJsqtqkD")
        .CollectionParameter<JS_B_教师其他情况D>("JsqtqkD");
            builder.EntityType<JS_B_教师其他情况E>().Collection
        .Action("BulkAddJsqtqkE")
        .CollectionParameter<JS_B_教师其他情况E>("JsqtqkE");
            builder.EntityType<JS_B_教师其他情况F>().Collection
        .Action("BulkAddJsqtqkF")
        .CollectionParameter<JS_B_教师其他情况F>("JsqtqkF");
            builder.EntityType<JS_B_教师其他情况G>().Collection
        .Action("BulkAddJsqtqkG")
        .CollectionParameter<JS_B_教师其他情况G>("JsqtqkG");
            builder.EntityType<PG_B_ZDZDCX>().Collection
        .Action("BulkAddZdzdcx")
        .CollectionParameter<PG_B_ZDZDCX>("Zdzdcx");
            builder.EntityType<PG_B_JZXQK>().Collection
        .Action("BulkAddJzxqk")
        .CollectionParameter<PG_B_JZXQK>("Jzxqk");
            builder.EntityType<PG_B_JXYJJBQK>().Collection
        .Action("BulkAddJxyjjbqk")
        .CollectionParameter<PG_B_JXYJJBQK>("Jxyjjbqk");
            builder.EntityType<PG_B_DDRYJBQK>().Collection
        .Action("BulkAddDdryjbqk")
        .CollectionParameter<PG_B_DDRYJBQK>("Ddryjbqk");

        builder.EntityType<JS_B_教师基本情况>().Collection
        .Action("BulkAddJsjbqk")
        .CollectionParameter<JS_B_教师基本情况>("Jsjbqk");

            builder.EntitySet<PG_B_ZDJZMJ>("Zdjzmjs");

            builder.EntityType<PG_B_ZDJZMJ>().Collection
        .Action("BulkAddZdjzmj")
        .CollectionParameter<PG_B_ZDJZMJ>("Zdjzmj");

            builder.EntitySet<PG_B_GZTS>("Gztss");

            builder.EntityType<PG_B_GZTS>().Collection
        .Action("BulkAddGzts")
        .CollectionParameter<PG_B_GZTS>("Gzts");

            builder.EntitySet<PG_B_YLS>("Ylss");

            builder.EntityType<PG_B_YLS>().Collection
        .Action("BulkAddYls")
        .CollectionParameter<PG_B_YLS>("Yls");

            builder.EntitySet<PG_B_XXHJSGK>("Xxhjsgks");

            builder.EntityType<PG_B_XXHJSGK>().Collection
        .Action("BulkAddXxhjsgk")
        .CollectionParameter<PG_B_XXHJSGK>("Xxhjsgk");

            builder.EntitySet<PG_B_GLXXXT>("Glxxxts");

            builder.EntityType<PG_B_GLXXXT>().Collection
        .Action("BulkAddGlxxxt")
        .CollectionParameter<PG_B_GLXXXT>("Glxxxt");

            builder.EntitySet<PG_B_XXHJG>("Xxhjgs");

            builder.EntityType<PG_B_XXHJG>().Collection
        .Action("BulkAddXxhjg")
        .CollectionParameter<PG_B_XXHJG>("Xxhjg");

            builder.EntitySet<PG_B_GDZC>("Gdzcs");

            builder.EntityType<PG_B_GDZC>().Collection
        .Action("BulkAddGdzc")
        .CollectionParameter<PG_B_GDZC>("Gdzc");

            builder.EntitySet<PG_B_XNJD>("Xnjds");

            builder.EntityType<PG_B_XNJD>().Collection
        .Action("BulkAddXnjd")
        .CollectionParameter<PG_B_XNJD>("Xnjd");

            builder.EntitySet<PG_B_XWJD>("Xwjds");

            builder.EntityType<PG_B_XWJD>().Collection
        .Action("BulkAddXwjd")
        .CollectionParameter<PG_B_XWJD>("Xwjd");

            builder.EntitySet<PG_B_JNJD>("Jnjds");

            builder.EntityType<PG_B_JNJD>().Collection
        .Action("BulkAddJnjd")
        .CollectionParameter<PG_B_JNJD>("Jnjd");

            builder.EntitySet<PG_B_XSJYXX>("Xsjyxxs");
            builder.EntitySet<PG_B_CXQK>("Cxqks");

            builder.EntityType<JS_B_在校生信息>().Collection
       .Action("BulkAddZxsxx")
       .CollectionParameter<JS_B_在校生信息>("Zxsxx");

            builder.EntityType<PG_B_XSJYXX>().Collection
          .Action("BulkAddXsjy")
          .CollectionParameter<PG_B_XSJYXX>("Xsjyxx");
            builder.EntityType<PG_B_ZSQK>().Collection
            .Action("BulkAddZsqk")
            .CollectionParameter<PG_B_ZSQK>("Xsjy");
            builder.EntityType<PG_B_JYL>().Collection
            .Action("BulkAddJyl")
            .CollectionParameter<PG_B_JYL>("Xsjy");
            builder.EntityType<PG_B_SHJZ>().Collection
            .Action("BulkAddShjz")
            .CollectionParameter<PG_B_SHJZ>("Xsjy");
            builder.EntityType<PG_B_JYDW>().Collection
            .Action("BulkAddJydw")
            .CollectionParameter<PG_B_JYDW>("Xsjy");
            builder.EntityType<PG_B_ZLGC>().Collection
            .Action("BulkAddZlgc")
            .CollectionParameter<PG_B_ZLGC>("Xsjy");
            builder.EntityType<PG_B_XSHJ>().Collection
            .Action("BulkAddXshj")
            .CollectionParameter<PG_B_XSHJ>("Xsjy");
            builder.EntityType<PG_B_XXHJ>().Collection
            .Action("BulkAddXxhj")
            .CollectionParameter<PG_B_XXHJ>("Xsjy");
            builder.EntityType<PG_B_STHJ>().Collection
            .Action("BulkAddSthj")
            .CollectionParameter<PG_B_STHJ>("Xsjy");

            builder.EntityType<PG_B_CXXS>().Collection
            .Action("BulkAddCxxs")
            .CollectionParameter<PG_B_CXXS>("Cxxs");

            builder.EntityType<PG_B_XSST>().Collection
           .Action("BulkAddXsst")
           .CollectionParameter<PG_B_XSST>("Xsst");

            builder.EntityType<PG_B_ZYZ>().Collection
            .Action("BulkAddZyz")
            .CollectionParameter<PG_B_ZYZ>("Zyz");

            builder.EntityType<PG_B_BCSM>().Collection
          .Action("BulkAddBcsm")
          .CollectionParameter<PG_B_BCSM>("Bcsm");

            builder.EntityType<PG_B_KSZY_New>().Collection
          .Action("BulkAddKszy_new")
          .CollectionParameter<PG_B_KSZY_New>("Kszy_new");

            builder.EntityType<PG_B_XSJBXX_New>().Collection
          .Action("BulkAddXsjbxx_new")
          .CollectionParameter<PG_B_XSJBXX_New>("Xsjbxx_new");

            builder.EntitySet<PG_B_CXXS>("Cxxss");
            builder.EntitySet<PG_B_YJJYQK>("Yjjyqks");
            builder.EntitySet<PG_B_XSST>("Xssts");
            builder.EntitySet<PG_B_HSZH>("Hszhs");
            builder.EntitySet<PG_B_ZYZ>("Zyzs");
            builder.EntitySet<PG_B_JXGL>("Jxgls");

            builder.EntityType<PG_B_JXGL>().Collection
        .Action("BulkAddJxgl")
        .CollectionParameter<PG_B_JXGL>("Jxgl");

            builder.EntitySet<PG_B_DNZYBDQK>("Dnzybdqks");
            builder.EntitySet<PG_B_ZXXSDQHJMZQK>("Zxxsdqhjmzqks");
            builder.EntitySet<PG_B_FZTYJR>("Fztyjrs");
            builder.EntitySet<PG_B_KSZY_New>("Kszy_news");
            builder.EntitySet<PG_B_XSJBXX_New>("Xsjbxx_news");
            builder.EntitySet<PG_B_BCSM>("Bcsms");
            builder.EntitySet<PG_B_XSHJ>("Xshj");
            builder.EntitySet<PG_B_XXHJ>("Xxhj");
            builder.EntitySet<PG_B_STHJ>("Sthj");
            builder.EntitySet<PG_B_ZLGC>("Zlgc");
            builder.EntitySet<PG_B_JYDW>("Jydw");
            builder.EntitySet<PG_B_SHJZ>("Shjz");
            builder.EntitySet<PG_B_ZSQK>("Zsqk");
            builder.EntitySet<PG_B_JYL>("Jyl");

            builder.EntitySet<PG_B_JXGLJBQK>("Jxgljbqks");

            builder.EntityType<PG_B_JXGLJBQK>().Collection
        .Action("BulkAddJxgljbqk")
        .CollectionParameter<PG_B_JXGLJBQK>("Jxgljbqk");


            builder.EntitySet<PG_B_XSGLJBQK>("Xsgljbqks");
            
            builder.EntityType<PG_B_XSGLJBQK>().Collection
        .Action("BulkAddXsgljbqk")
        .CollectionParameter<PG_B_XSGLJBQK>("Xsgljbqk");

            builder.EntitySet<PG_B_ZSJYJBQK>("Zsjyjbqks");

            builder.EntityType<PG_B_ZSJYJBQK>().Collection
                    .Action("BulkAddZsjyjbqk")
                    .CollectionParameter<PG_B_ZSJYJBQK>("Zsjyjbqk");

            builder.EntitySet<PG_B_DDRYJBQK>("Ddryjbqks");
            builder.EntitySet<PG_B_JXYJJBQK>("Jxyjjbqks");
            builder.EntitySet<PG_B_PJQK>("Pjqks");
            builder.EntitySet<PG_B_JZXQK>("Jzxqks");
            builder.EntitySet<PG_B_ZDZDCX>("Zdzdcxs");
            builder.EntitySet<PG_B_ZYDTR>("Zydtrs");

            builder.EntityType<PG_B_ZYDTR>().Collection
        .Action("BulkAddZydtr")
        .CollectionParameter<PG_B_ZYDTR>("Zydtr");

            builder.EntitySet<PG_B_ZYFZR>("Zyfzrs");

            builder.EntityType<PG_B_ZYFZR>().Collection
        .Action("BulkAddZyfzr")
        .CollectionParameter<PG_B_ZYFZR>("Zyfzr");

            builder.EntitySet<PG_B_ZYZGZS>("Zyzgzss");

            builder.EntityType<PG_B_ZYZGZS>().Collection
        .Action("BulkAddZyzgzs")
        .CollectionParameter<PG_B_ZYZGZS>("Zyzgzs");

            builder.EntitySet<PG_B_SHPX>("Shpxs");

            builder.EntityType<PG_B_SHPX>().Collection
        .Action("BulkAddShpx")
        .CollectionParameter<PG_B_SHPX>("Shpx");

            builder.EntitySet<PG_B_DGSX>("Dgsxs");

            builder.EntityType<PG_B_DGSX>().Collection
        .Action("BulkAddDgsx")
        .CollectionParameter<PG_B_DGSX>("Dgsx");

            builder.EntitySet<PG_B_CXHZ>("Cxhzs");

            builder.EntityType<PG_B_CXHZ>().Collection
        .Action("BulkAddCxhz")
        .CollectionParameter<PG_B_CXHZ>("Cxhz");

            builder.EntitySet<PG_B_ZS>("Zss");

            builder.EntityType<PG_B_ZS>().Collection
       .Action("BulkAddZs")
       .CollectionParameter<PG_B_ZS>("Zs");

            builder.EntitySet<PG_B_SJJYQK>("Sjjyqks");

            builder.EntityType<PG_B_SJJYQK>().Collection
        .Action("BulkAddSjjyqk")
        .CollectionParameter<PG_B_SJJYQK>("Sjjyqk");

            builder.EntitySet<JS_B_在校生信息>("Zxsxx");
            builder.EntitySet<JS_B_开设专业>("Kszy");

            builder.EntityType<JS_B_开设专业>().Collection
        .Action("BulkAddKszy")
        .CollectionParameter<JS_B_开设专业>("Kszy");


            //
            builder.EntitySet<PG_B_XTZ>("Xtzs");

            builder.EntityType<PG_B_XTZ>().Collection
        .Action("BulkAddXtzs")
        .CollectionParameter<PG_B_XTZ>("Xtzs");
            //

            builder.EntitySet<JS_B_教学工作量>("Jxgzl");
            builder.EntitySet<JS_B_教师其他情况A>("JsqtqkA");
            builder.EntitySet<JS_B_教师其他情况B>("JsqtqkB");
            builder.EntitySet<JS_B_教师其他情况C>("JsqtqkC");
            builder.EntitySet<JS_B_教师其他情况D>("JsqtqkD");
            builder.EntitySet<JS_B_教师其他情况E>("JsqtqkE");
            builder.EntitySet<JS_B_教师其他情况F>("JsqtqkF");
            builder.EntitySet<JS_B_教师其他情况G>("JsqtqkG");
            builder.EntitySet<JS_B_教师其他情况汇总>("Jsqtqkhz");
            builder.EntitySet<JS_B_教师基本情况>("Jsjbqk");
            builder.EntitySet<JS_B_教师授课情况>("Jsskqk");
            builder.EntitySet<JS_B_机构设置>("Jgsz");
         
            builder.EntityType<JS_B_机构设置>().Collection
.Action("BulkAddJGSZ")
.CollectionParameter<JS_B_机构设置>("JGSZ");

            builder.EntitySet<JS_B_课程设置>("Kcsz");

            builder.EntityType<JS_B_课程设置>().Collection
.Action("BulkAddKcsz")
.CollectionParameter<JS_B_课程设置>("Kcsz");

            builder.EntitySet<SET_权限>("Right");
              builder.EntitySet<SET_TASK>("Task");
            builder.EntitySet<SET_LOCK>("Lock");
             builder.EntitySet<SET_登录日志>("LoginLog");
            return builder.GetEdmModel();
        }

    }
}
